package com.xuecheng.content.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanMediaVo;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mappers.TeachplanMapper;
import com.xuecheng.content.mappers.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.xuecheng.content.service.TeachplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanWorkService;
import com.xuecheng.media.client.MediaClient;
import com.xuecheng.web.exceptions.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-16
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;
    @Autowired
    private CourseBaseService courseBaseService;

    @Override
    public ResponseResult<TeachplanVo> treeNodes(Long courseId) {
//        LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
//        qw.eq(Teachplan::getCourseId,courseId);
//        List<Teachplan> list = this.list(qw);
//        List<TeachplanVo> vos = BeanHelper.copyWithCollection(list, TeachplanVo.class);
//        for (TeachplanVo teachplanVo : vos) {
//            LambdaQueryWrapper<TeachplanMedia> qw1 = new LambdaQueryWrapper<>();
//            qw1.eq(TeachplanMedia::getTeachplanId,teachplanVo.getId());
//            TeachplanMedia teachplanMedia = teachplanMediaMapper.selectOne(qw1);
//            if (teachplanMedia != null) {
//                TeachplanMediaVo teachplanMediaVo = BeanHelper.copyProperties(teachplanMedia, TeachplanMediaVo.class);
//                teachplanVo.setTeachplanMedia(teachplanMediaVo);
//            }
//        }

        List<TeachplanVo> vos=teachplanMediaMapper.findTreeNodes(courseId);

        TeachplanVo teachplanVo = vos.stream().filter(c -> c.getGrade() == 1).findFirst().get();
        List<TeachplanVo> collect = vos.stream().filter(c -> c.getGrade() == 2).collect(Collectors.toList());
        for (TeachplanVo vo : collect) {
            List<TeachplanVo> collect1 = vos.stream().filter(c -> c.getParentid().equals(vo.getId())).collect(Collectors.toList());
            vo.setTeachPlanTreeNodes(collect1);
        }
        teachplanVo.setTeachPlanTreeNodes(collect);
        return ResponseResult.okResult(teachplanVo);
    }



    @Override
    public ResponseResult saveOrUpdateTeachplan(TeachplanDto dto) {
        Long courseId = dto.getCourseId();
        CourseBase courseBase = courseBaseService.getById(courseId);
        String auditStatus = courseBase.getAuditStatus();
        if (!auditStatus.equals(CourseConstants.AUDIT_DISPAST_STATUS)&&
                !auditStatus.equals(CourseConstants.AUDIT_UNPAST_STATUS) ){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        Teachplan teachplan = BeanHelper.copyProperties(dto, Teachplan.class);
        boolean flag=false;
        if (dto.getId()==null){
            if (teachplan.getParentid()==null) {
                LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
                qw.eq(Teachplan::getGrade,1);
                qw.eq(Teachplan::getCourseId,teachplan.getCourseId());
                Teachplan parent = this.getOne(qw);
                teachplan.setParentid(parent.getId());
            }
            if (dto.getGrade() == 3) {
                courseBase.setPlanNum(courseBase.getPlanNum()+1);
                courseBaseService.updateById(courseBase);
            }
            flag=this.save(teachplan);
        }else {
            flag=this.updateById(teachplan);
        }
        if (!flag) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteTeachplan(Long id) {
        Teachplan teachplan = this.getById(id);
        Long courseId = teachplan.getCourseId();
        CourseBase courseBase = courseBaseService.getById(courseId);
        String auditStatus = courseBase.getAuditStatus();
        if (!auditStatus.equals(CourseConstants.AUDIT_DISPAST_STATUS)&&
                !auditStatus.equals(CourseConstants.AUDIT_UNPAST_STATUS) ){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        if (teachplan.getGrade() == 3) {
            courseBase.setPlanNum(courseBase.getPlanNum()-1);
            courseBaseService.updateById(courseBase);
        }else if (teachplan.getGrade() == 2) {
            LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
            qw.eq(Teachplan ::getParentid,teachplan.getId() );
            int count = (int) this.count(qw);
            courseBase.setPlanNum(courseBase.getPlanNum()-count);
            courseBaseService.updateById(courseBase);
            this.remove(qw);
        }else {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        boolean delete = this.removeById(id);
        if (!delete) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        return ResponseResult.okResult();
    }


}
